package component.show.containers
{
	import common.manager.Resource;
	
	import component.show.control.UISprite;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	public class ToggleButton extends UISprite
	{	
		private var m_skinName : String;
		
		private var m_selectedState : DisplayObject;
		private var m_releaseState : DisplayObject;
		private var m_overState : DisplayObject;
		
		private var m_selected : Boolean;
		
		private var m_splBtn : SimpleButton;
		
		private var m_autoSwitch : Boolean;
		
		public function ToggleButton(selectedState:DisplayObject=null, releaseState:DisplayObject=null,overState:DisplayObject=null,autoSwitch:Boolean= true)
		{
			drawState(releaseState);
			
			m_selectedState = selectedState;
			m_releaseState = releaseState;
			m_overState = overState;
			
			m_splBtn = new SimpleButton;
			
			this.addChild(m_splBtn);
			
			if(autoSwitch)
			{
				this.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			}
		}
		
		public override function  destory() : void
		{
			super.destory();
			
			this.removeEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			
			m_skinName = null;
			m_selectedState = null;
			m_releaseState = null;
			m_overState = null;
		}
		
		public function set autoSwicth(value : Boolean) : void
		{
			m_autoSwitch = value;
			
			if(this.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				this.removeEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);	
			}
			
			if(m_autoSwitch)
			{
				this.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			}
		}
		
		public function get autoSwicth() : Boolean
		{
			return m_autoSwitch;
		}
		
		private function OnMouseDown(evt : MouseEvent) : void
		{
			m_selected = !m_selected;
			
			swicthState();
		}
		
		/**
		 * 设置状态
		 * @param value 
		 */		
		public function set selected(value : Boolean) : void
		{
			m_selected = value;
			
			swicthState();
		}
		
		/**
		 * 切换状态 
		 */		
		private function swicthState() : void
		{
			if(m_selected)
			{
				drawState(m_selectedState);
			}
			else
			{
				drawState(m_releaseState);
			}
		}
		
		/**
		 * 绘制状态
		 * @param state 
		 */		
		private function drawState(state : DisplayObject) : void
		{
			if(!state) return;
			
			m_splBtn.downState = state;
			m_splBtn.overState = m_overState;
			m_splBtn.upState = state;
		}
		
		public function set skinName(name : String) : void
		{
			m_skinName = name;
			
			var clas : Class = Resource.getClass(name,"UISkin");
			
			simpleButtonRenderer = clas;
		}
		
		public function get skinName() : String
		{
			return m_skinName;
		}
		
		public function set simpleButtonRenderer(spleBtnClass : Class) : void
		{
			var sbtn : SimpleButton = new spleBtnClass as SimpleButton;
			
			m_releaseState = sbtn.upState;
			m_selectedState = sbtn.downState;
			m_overState = sbtn.overState;
			
			drawState(m_releaseState);
			
			this.width = sbtn.width;
			this.height = sbtn.height;
		}
		
		public override function drawBckgound():void
		{
			
		}
	}
}